doctype
html.login-bg
    head
        include ../include/meta
        title #{title}
        include ../include/bootstrap
        include ../include/globalStyle

        <!-- libraries -->
        link(href="/vendor/css/lib/font-awesome.css",rel="stylesheet")
        <!-- this page specific styles -->
        link(href="/vendor/css/compiled/user-list.css",rel="stylesheet",media="screen")
        <!-- open sans font -->
        link(href="http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,700italic,800italic,400,300,600,700,800",rel="stylesheet",media="screen")


    body
        include ../include/navbar
        include ../include/sidebar
        .content
            .container-fluid
                .users-list#pad-wrapper
                    .row-fluid.header
                        h3 管理人员列表
                    .row-fluid.table
                        table.table.table-hover
                            thead
                                tr
                                    th 用户名
                                    th 昵称
                                    th 手机
                                    th 等级
                                    th 渠道
                                    th 是否被禁用
                                    th 查看
                                    th 修改
                                    th 删除
                            tbody
                                if users && users.length > 0
                                    each item in users
                                        tr(class="item-id-" + item._id)
                                            td #{item.name}
                                            td #{item.nickName}
                                            td #{item.phone}
                                            td #{item.level}
                                            if(item.way && item.way.name)
                                                td #{item.way.name}
                                            else
                                                td 无渠道
                                            td
                                                if(item.forbidden)
                                                    button.btn.btn-success.change(type="button",data-id=item._id) 解禁
                                                else
                                                    button.btn.btn-warning.change(type="button",data-id=item._id) 禁用
                                            td: a(target="_blank", href= "/admin/userdetail/" + item._id) 查看
                                            td: a(target="_blank", href= "/admin/userupdate/" + item._id) 修改
                                            td: button.btn.btn-danger.del(type="button", data-id=item._id) 删除
                    .pagination.pull-right
                        ul
                            - var n=1
                            while n <= totalPageNum
                                li: a(href="/admin/userlist/" + n)=n++


    script(src="/vendor/js/jquery-3.2.1.min.js")
    script(src="/vendor/js/bootstrap.min.js")
    script(src="/vendor/js/theme.js")
    script.
        $('.del').click(function (e) {

            var target = $(e.target);
            var id = target.data('id');
            var tr = $('.item-id-' + id);

            if (tr.find('.change').html() === '禁用') {
                alert('先将用户禁用才能删除');
                return;
            }

            $.ajax({
                type: 'DELETE',
                url: '/admin/user/delete?id=' + id
            })
                .done(function (results) {
                    if (results.state === 'success') {
                        if (tr.length > 0) {
                            tr.remove()
                        }
                    }
                })
        });

        $('.change').click(function (e) {
            var target = $(e.target);
            var id = target.data('id');
            var word = target.html();
            if (word === '禁用') {
                if (confirm('确认是否' + word)) {
                    $.ajax({
                        type: 'GET',
                        url: '/admin/user/forbidden?id=' + id
                    })
                        .done(function (results) {
                            console.log(results);
                            if (results.state === 'success') {
                                if (results.forbidden) {
                                    target.removeClass('btn-warning').addClass('btn-success').html('解禁');
                                } else {
                                    target.removeClass('btn-success').addClass('btn-warning').html('禁用');
                                }
                            }
                        })
                }
            } else {
                $.ajax({
                    type: 'GET',
                    url: '/admin/user/forbidden?id=' + id
                })
                    .done(function (results) {
                        console.log(results);
                        if (results.state === 'success') {
                            if (results.forbidden) {
                                target.removeClass('btn-warning').addClass('btn-success').html('解禁');
                            } else {
                                target.removeClass('btn-success').addClass('btn-warning').html('禁用');
                            }
                        }
                    })
            }
        });
